package com.googlecode.contests_repo.servlets;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.googlecode.contests_repo.core.config.ConfigurationManager;

/**
 * A servlet creating a new folder in a given location.
 */
@WebServlet("/new_folder")
public class CreateNewFolderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String encodedLocation = request.getParameter("location");
		// String location = URLDecoder.decode(encodedLocation, "UTF-8");
		String location = encodedLocation;
		String folderName = request.getParameter("folderName");
		if (folderName != null) {
			String repoRoot = ConfigurationManager.getInstance().getRepoRoot();
			String absolutePath = repoRoot + File.separator + location
					+ File.separator + folderName;
			// boolean success = new File(absolutePath).mkdir();
			new File(absolutePath).mkdir();
		}
		// response.sendRedirect("view_folder.jsp?path=" + encodedLocation +
		// "&folderCreated=" + success);
		response.sendRedirect("resource.jsp?path=" + encodedLocation
				+ "&edit=1");
	}
}
